#!/usr/bin/env julia

"""
Test runner for TongLun.jl

This script:
1. Sets up the required environment variables
2. Runs the comprehensive test suite
3. Reports results

Usage:
    julia run_tests.jl
    
Or with specific library path:
    TONGLUN_CORELIB_PATH=/path/to/libtonglun_core.dylib julia run_tests.jl
"""

using Pkg
using Test

println("TongLun.jl Test Runner")
println("=" ^ 50)

# Check if environment variable is set
if !haskey(ENV, "TONGLUN_CORELIB_PATH")
    println("⚠️  TONGLUN_CORELIB_PATH not set, using default path...")
    default_path = "/Users/thaut/OSS/tonglun/target/debug/libtonglun_core.dylib"
    ENV["TONGLUN_CORELIB_PATH"] = default_path
    println("   Using: $default_path")
else
    println("✅ Using TONGLUN_CORELIB_PATH: $(ENV["TONGLUN_CORELIB_PATH"])")
end

# Verify library exists
library_path = ENV["TONGLUN_CORELIB_PATH"]
if !isfile(library_path)
    println("❌ Library file not found at: $library_path")
    println()
    println("Please either:")
    println("1. Build the Rust library: cargo build --debug")
    println("2. Set TONGLUN_CORELIB_PATH to the correct path")
    println()
    println("Example:")
    println("  export TONGLUN_CORELIB_PATH=/Users/thaut/OSS/tonglun/target/debug/libtonglun_core.dylib")
    println("  julia run_tests.jl")
    exit(1)
end

println("✅ Library file found")
println()

# Ensure we're in the right directory
cd(dirname(@__FILE__))

# Add the current directory to LOAD_PATH so we can load TongLun
push!(LOAD_PATH, pwd())

println("🚀 Starting tests...")
println()

# Run the test suite
try
    # Force recompilation to ensure we get the latest changes
    println("📦 Loading TongLun module...")
    
    # Run the comprehensive tests
    include("test/runtests.jl")
    
    println()
    println("=" ^ 50)
    println("🎉 All tests completed successfully!")
    
catch e
    println()
    println("=" ^ 50)
    println("❌ Tests failed with error:")
    println(e)
    
    if isa(e, InterruptException)
        println("Tests were interrupted by user")
    else
        println()
        println("Stacktrace:")
        for (exc, bt) in Base.catch_stack()
            showerror(stdout, exc, bt)
            println()
        end
    end
    
    exit(1)
end 